package com.prs.crm.action.oa;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import com.prs.crm.annotation.Clew;
import com.prs.crm.domain.hr.Dept;
import com.prs.crm.domain.oa.CompanyMailList;
import com.prs.crm.service.hr.DeptService;
import com.prs.crm.service.oa.CompanyMailListService;

public class CompanyMailListAction extends BaseAction{
	private static final long serialVersionUID = 1L;
	private PaginationSupport pagination;
	private Integer[] ids;
	private CompanyMailList companyMailList;
	private CompanyMailListService companyMailListService;
	private DeptService deptService;
	private String birthday;
	
	@Action("listCompanyMailList")
	public String listCompanyMailList(){
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				CompanyMailList.class);
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getCompanyMailListService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}
	
	@Action("selectDept")
	public String selectDept(){
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				Dept.class);
		Dept dept = this.getDeptService().getDeptByName("集团");
		criteria.add(Restrictions.eq("parent", dept));
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getDeptService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}
	
	@Action("editCompanyMailList")
	public String editCompanyMailList(){
		return SUCCESS;
	}
	
	@Action("viewCompanyMailList")
	public String viewCompanyMailList(){
		return SUCCESS;
	}
	
	@Action(value = "removeCompanyMailList", results = {
			@Result(name = "success", type = "redirect", location = "listCompanyMailList"),
			@Result(name = "error", type = "chain", location = "listCompanyMailList") })
	public String removeCompanyMailList() {
		if (ids == null) {
			this.addActionError("请选择要删除的通讯录人员");
			return ERROR;
		}
		this.getCompanyMailListService().batchRemove(ids);
		return SUCCESS;
	}
	
	@Clew(results = { "success" })
	@Action(value = "saveCompanyMailList", results = {
			@Result(name = "success", type = "redirect", location = "listCompanyMailList"),
			@Result(name = "error", type = "chain", location = "editCompanyMailList") })
	public String saveCompanyMailList() {
		System.err.println("出生日期:" + birthday);
		if (companyMailList.getName() == null || "".equals(companyMailList.getName().trim())) {
			addActionError("姓名不能为空");
			return ERROR;
		}
		if (companyMailList.getDept() == null) {
			addActionError("部门不能为空");
			return ERROR;
		}
		companyMailList.setBirthday(birthday);
		this.setPrompt("创建通讯录成功");
		this.getCompanyMailListService().save(companyMailList);
		return SUCCESS;
	}
	
	public PaginationSupport getPagination() {
		return pagination;
	}
	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}
	public Integer[] getIds() {
		return ids;
	}
	public void setIds(Integer[] ids) {
		this.ids = ids;
	}
	public CompanyMailList getCompanyMailList() {
		return companyMailList;
	}
	public void setCompanyMailList(CompanyMailList companyMailList) {
		this.companyMailList = companyMailList;
	}
	public CompanyMailListService getCompanyMailListService() {
		return companyMailListService;
	}
	public void setCompanyMailListService(
			CompanyMailListService companyMailListService) {
		this.companyMailListService = companyMailListService;
	}

	public DeptService getDeptService() {
		return deptService;
	}

	public void setDeptService(DeptService deptService) {
		this.deptService = deptService;
	}

	public String getBirthday() {
		return birthday;
	}

	public void setBirthday(String birthday) {
		this.birthday = birthday;
	}
}
